package com.tm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.tm.ResultEntity;
import com.tm.model.EcMemberInfo;
import com.tm.service.CommonService;
import com.tm.service.EcMemberInfoService;
import com.tm.util.CoreController;
import com.tm.util.DateUtilsEx;
import com.tm.util.JSONUtilsEx;
import com.tm.util.ServletUtilsEx;
import com.tm.util.StringUtilsEx;
import com.tm.util.TaobaoSmsUtilEx;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * 会员信息管理相关接口
 * 
 * @author GXL
 *
 * restful风格返回
 *
 */
@RestController
@RequestMapping(value = "/member")
public class EcMemberInfoController extends CoreController {

	private Logger logger = Logger.getLogger(EcMemberInfoController.class);
	
	@Autowired
	private EcMemberInfoService emiService;
	
	@Autowired
	private CommonService cService;
	
	/**
	 * H5手机获取验证码
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="H5手机获取验证码",notes="H5会员手机获取验证码接口")
    @RequestMapping(value="/sendsms/{mobile}/{type}", method=RequestMethod.GET)
    public ResultEntity sendsms(@PathVariable String mobile, @PathVariable String type,
    		HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------H5获取验证码接口begin-----------------------------------------");
		try {
				EcMemberInfo tm = null;
					
				//判断该手机号是否已注册会员
				tm = emiService.getMemberByMobile(mobile);
				
				if("1".equals(type) && tm != null){
					re = new ResultEntity("-1", "该手机号已被注册！");
					return re;
				}
				
				String mobilecode=StringUtilsEx.RandomNumString(6);
				Map<String, String> param = new HashMap<String, String>();
				param.put("code", mobilecode);
				
				/*SendSmsResponse res = null;
				
				res = AliyunSmsUtilEx.sendSms(mobile, "SMS_128885356", JSONUtilsEx.serialize(param));
				
				System.out.println("短信接口返回的数据----------------");
		        System.out.println("Code=" + res.getCode());
		        System.out.println("Message=" + res.getMessage());
				
				if("OK".equals(res.getCode())){*/
				AlibabaAliqinFcSmsNumSendResponse res = null;
				
				res = TaobaoSmsUtilEx.sendSms(mobile, mobilecode, "SMS_10145678");
				
				System.out.println("短信接口返回的数据----------------");
		        System.out.println("ErrorCode=" + res.getErrorCode());
		        System.out.println("ErrorMessage=" + res.getMsg());
				
				if(res.getErrorCode() == null){
					
					Integer memberId = (Integer) request.getSession().getAttribute("memberId");
					
					tm = emiService.getmemberByMemberId(memberId);
					tm.setEditTime(mobile);
					tm.setMobileCode(mobilecode);
					re = emiService.updateMember(tm);
				
				}else{
					re = new ResultEntity("-1", "短信发送失败!");
				}
				
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			returnRs("-1", "参数类型有误！", response);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			returnRs("-1", msg, response);
		}
		System.out.println("-------------------------------H5获取验证码接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 设置默认地址
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="设置默认地址",notes="设置默认地址接口")
    @RequestMapping(value="/setAddr", method=RequestMethod.POST)
    public ResultEntity setAddr(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------设置默认地址接口begin-----------------------------------------");
		try {
			
			Integer addrId = Integer.parseInt(request.getParameter("addrId"));//地址id
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			re = emiService.setAddr(addrId, memberId);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------设置默认地址接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 删除地址
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="删除地址",notes="删除地址接口")
    @RequestMapping(value="/delAddr", method=RequestMethod.POST)
    public ResultEntity delAddr(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------删除地址接口begin-----------------------------------------");
		try {
			
			Integer addrId = Integer.parseInt(request.getParameter("addrId"));//地址id
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			re = emiService.delAddr(addrId, memberId);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------删除地址接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 新增地址
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="新增地址",notes="新增地址接口")
    @RequestMapping(value="/addAddr", method=RequestMethod.POST)
    public ResultEntity addAddr(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------新增地址接口begin-----------------------------------------");
		try {
			
			Integer provinceId = Integer.parseInt(request.getParameter("provinceId"));
			Integer cityId = Integer.parseInt(request.getParameter("cityId"));
			Integer areaId = Integer.parseInt(request.getParameter("areaId"));
			String deliveryReceiver = request.getParameter("deliveryReceiver");
			String deliveryMobile = request.getParameter("deliveryMobile");
			String deliveryAddress = request.getParameter("deliveryAddress");
			String isDefault = request.getParameter("isDefault");
			
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			re = emiService.addAddr(deliveryReceiver, deliveryMobile, deliveryAddress, provinceId, cityId, areaId, memberId, isDefault);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------新增地址接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 编辑地址
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="编辑地址",notes="编辑地址接口")
    @RequestMapping(value="/updateAddr", method=RequestMethod.POST)
    public ResultEntity updateAddr(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------编辑地址接口begin-----------------------------------------");
		try {
			
			Integer addrId = Integer.parseInt(request.getParameter("addrId"));
			Integer provinceId = Integer.parseInt(request.getParameter("provinceId"));
			Integer cityId = Integer.parseInt(request.getParameter("cityId"));
			Integer areaId = Integer.parseInt(request.getParameter("areaId"));
			String deliveryReceiver = request.getParameter("deliveryReceiver");
			String deliveryMobile = request.getParameter("deliveryMobile");
			String deliveryAddress = request.getParameter("deliveryAddress");
			String isDefault = request.getParameter("isDefault");
			
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			re = emiService.updateAddr(addrId, deliveryReceiver, deliveryMobile, deliveryAddress, provinceId, cityId, areaId, memberId, isDefault);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------编辑地址接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 获取发展客户列表
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="获取发展客户列表",notes="获取发展客户列表接口")
    @RequestMapping(value="/lowerList", method=RequestMethod.POST)
    public ResultEntity lowerList(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------获取发展客户列表接口begin-----------------------------------------");
		try {
			
			Integer startIndex = Integer.parseInt(request.getParameter("startIndex"));
			Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			re = emiService.getLowerListByMemberId(memberId, startIndex, pageSize);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------获取发展客户列表接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 绑定手机接口
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "绑定手机", notes = "绑定手机接口")
	@RequestMapping(value = "/bindMobile", method = RequestMethod.POST)
	public ResultEntity bindMobile(HttpServletRequest request, HttpServletResponse response) {
		ResultEntity re = null;
		String msg = "";
		System.out.println("-------------------------------绑定手机接口begin-----------------------------------------");
		try {
			
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			String mobile = request.getParameter("mobile");
			String vcode = request.getParameter("vcode");
			
			EcMemberInfo tmi = emiService.getmemberByMemberId(memberId);
			
			if(!mobile.equals(tmi.getEditTime())){
				re = new ResultEntity("-1", "手机号码错误！");
				return re;
			}
			
			if(vcode.equals(tmi.getMobileCode())){
				tmi.setMobile(mobile);
				tmi.setEditTime(DateUtilsEx.formatToString(new Date(), DateUtilsEx.DATE_FORMAT_SEC));
				re = emiService.updateMember(tmi);
			}else{
				re = new ResultEntity("-1", "验证码错误！");
			}
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------绑定手机接口end-----------------------------------------");
		
		return re;
	}
	
	/**
     * 会员签到领取金币
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/addSignIn", method=RequestMethod.POST)
    public ResultEntity addSignIn(HttpServletRequest request, HttpServletResponse response) {
    	ResultEntity re = null;
    	String msg="";
		System.out.println("-------------------------------会员签到领取金币接口begin-----------------------------------------");
		try {
			
			Integer memberId =(Integer)request.getSession().getAttribute("memberId");//会员ID
			re = emiService.addMemberSignLog(memberId);
			
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg, response);
		}
		System.out.println("-------------------------------会员签到领取金币接口end-----------------------------------------");
		
		return re;
	}
    
    /**
	 * 设置会员类型接口
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "设置会员类型", notes = "设置会员类型接口")
	@RequestMapping(value = "/setMemberType", method = RequestMethod.POST)
	public ResultEntity setMemberType(HttpServletRequest request, HttpServletResponse response) {
		ResultEntity re = null;
		String msg = "";
		System.out.println("-------------------------------设置会员类型接口begin-----------------------------------------");
		try {
			
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			String xjMemberId = request.getParameter("memberId");
			String type = request.getParameter("type");
			
			re = emiService.setMemberType(memberId, xjMemberId, type);
			
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------设置会员类型接口end-----------------------------------------");
		
		return re;
	}
	
}
